import platform
import tkinter as tk
from tkinter import filedialog
import grade
import util
import os


class GradeGui(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title("电信学院学生学科成绩计算程序")
        # self.geometry("700x900")
        util.center_window(self, 800, 500)
        self.resizable(False, False)

        self.standard_font = (None, 16)

        self.main_frame = tk.Frame(self, width=500, height=300, bg="lightgrey")

        self.button_frame = tk.Frame(self, width=500, height=100)

        self.start_button = tk.Button(self.button_frame, text="开始计算", bg="lightgrey", fg="black",
                                      command=self.start, font=self.standard_font, state="disabled")
        self.start_button.pack(side=tk.RIGHT)

        self.open_file_button = tk.Button(self.button_frame, text="选择文件", bg="lightgrey", fg="black",
                                          command=self.open_file,
                                          font=self.standard_font)
        self.open_file_button.pack(side=tk.LEFT)

        self.button_frame.pack(side=tk.BOTTOM, pady=10)

        # self.file_name_v = tk.StringVar()
        # self.file_name_label = tk.Label(self.main_frame, textvariable=self.file_name_v, bg="lightgrey", fg="black",
        #                                 font=self.standard_font).pack(pady=0)


        # self.msg_v = tk.StringVar()
        # self.msg_label = tk.Label(self.main_frame, textvariable=self.msg_v, bg="lightgrey", fg="black",
        #                           font=self.standard_font).pack(fill=tk.X, pady=0)

        # , state=tk.DISABLED
        self.output_text = tk.Text(self.main_frame, bg="white", fg="black")
        self.output_text.pack(side=tk.TOP, expand=1, pady=10)

        self.output_text.delete('1.0', tk.END)
        self.output_text['state'] = tk.DISABLED

        self.main_frame.pack(fill=tk.BOTH, expand=1)
        self.protocol("WM_DELETE_WINDOW", self.safe_destroy)

    # 选择文件
    def open_file(self):
        self.output_text['state'] = tk.NORMAL
        if (platform.system() == 'Windows'):
            self.file_name = filedialog.askopenfilename(title="选择文件",
                                                        filetypes=(("xlsx files", "*.xlsx"), ("all files", "*.*")))
        else:
            self.file_name = filedialog.askopenfilename(title="选择文件",
                                                        filetypes=(("xlsx files", "*.xlsx"), ("all files", "*.*")))

        if (os.path.exists(self.file_name)):
            print(self.file_name)
            self.start_button['state'] = tk.NORMAL
            self.output_text.insert(tk.INSERT, "打开文件: {} \n".format(self.file_name))
            self.output_text.insert(tk.INSERT, "请点击开始计算按钮 \n")
        else:
            print("文件不存在或打开错误")
            self.output_text.insert(tk.INSERT, "文件不存在或打开错误，请重新打开文件 \n")
            # self.file_name_v.set(self.file_name)
        self.output_text['state'] = tk.DISABLED

    def start(self):
        self.output_text['state'] = tk.NORMAL
        count, save_path = grade.compute_grade(self.file_name)
        print("save_path: ", save_path)
        self.output_text.insert(tk.INSERT, "{}位学生学科成绩计算已完成。\n".format(count))
        self.output_text.insert(tk.INSERT, "结果文件保存路径为：" + save_path)
        
        if (platform.system() == 'Windows'):
            self.output_text.insert(tk.INSERT, "\r\n")
            self.output_text.insert(tk.INSERT, "\n")
        else:
            self.output_text.insert(tk.INSERT, "\r\n")
        self.start_button['state'] = tk.DISABLED
        self.output_text['state'] = tk.DISABLED

    def safe_destroy(self):
        self.destroy()


if __name__ == "__main__":
    gui = GradeGui()
    gui.mainloop()
